// pages/Projects/ProjectDetail/ProjectDetail.js

const app = getApp();

import { Login } from '../../../store/user';
import { queryProjectDetail , receiveProject , signProject } from '../../../api/project';
import { register } from '../../../api/login';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectId: '',
    showGetPhoneBtn: false,
    showRealNameBtn: false,
    project: {
      projectName: '', // 项目名称
      state: '', // 状态
      cost: '', // 预估费用
      createTime: '', // 创建时间
      remark: '', // 描述
      customerName: '',
    },
    shareTitle: app.globalData.global.shareTitle,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'项目详情')
    let _this = this;
    if(options.q){
      console.log(decodeURIComponent(options.q),'options.q')
      var q = decodeURIComponent(options.q)
      var list = q.split('=')
      var scene = '';
      if(list.length > 0){
        scene = list[1]
      }
      console.log(scene,'scene')
      _this.setData({
        projectId: scene,
      })
    }
    if(options.projectId){
      _this.setData({
        projectId: options.projectId,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function() {
    let _this = this;
    _this.willGetData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Tabbar/Home/Home?scene='+userId,
    }
  },
  // 获取项目详情信息
  getDetail(){
    let _this = this;
    queryProjectDetail(_this.data.projectId)
    .then(res => {
      if(res.success){
        _this.setData({
          project: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 去实名
  goRealname(){
    wx.navigateTo({
      url: '/pages/Tabbar/My/realName/realName',
    })
  },
  // 领取任务或签约项目
  goNext(){
    if(this.data.project.state == null){
      // 领取任务
      this.receiveProjectEvent();
    }else{
      // 去签约
      this.signProjectEvent();
    }
  },
  // 领取项目
  receiveProjectEvent(){
    let _this = this;
    receiveProject(_this.data.projectId)
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '领取成功',
          icon: 'none',
        })
        _this.getDetail();
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取签约链接
  signProjectEvent(){
    let _this = this;
    signProject(_this.data.projectId)
    .then(res => {
      if(res.success){
        wx.navigateTo({
          url: '/pages/link/link/link?url=' + encodeURIComponent(res.data) + '&title=' + JSON.stringify(_this.data.project.projectName + '签约'),
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取授权手机号
  getPhoneNumber (e) {
    console.log(e.detail.code)
    let _this = this;
    var preUserId = wx.getStorageSync('preUserId');
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // console.log(res,'res')
        wx.setStorageSync('loginCode', res.code)
        const params = preUserId == '' ? {loginCode: res.code, phoneCode: e.detail.code} : {loginCode: res.code, phoneCode: e.detail.code,preUserId: preUserId}
        register(params)
        .then(async(res1) => {
          if(res1.success){
            await Login(res.code);
            _this.willGetData()
          }else{
            if(res1.errMessage == '当前手机号已注册' || res1.errMessage == '当前微信已注册'){
              await Login(res.code);
              _this.willGetData();
            }else{
              wx.showToast({
                title: res1.errMessage,
                icon: 'none',
              })
            }
          }
        })
      }
    })
  },
  // 初始化操作
  async willGetData(){
    let _this = this;
    if(wx.getStorageSync('hasNewUser') == 'true'){
      await Login({noToast: false});
      wx.setStorageSync('hasNewUser', 'falses')
    }
    if(wx.getStorageSync('token')){
      if(wx.getStorageSync('token') && wx.getStorageSync('realNameStatus') == '3'){
        _this.getDetail()
      }else if(!wx.getStorageSync('token')){
        _this.setData({
          showGetPhoneBtn: true,
        })
      }else if(wx.getStorageSync('realNameStatus') != '3'){
        _this.setData({
          showRealNameBtn: true,
        })
      }
    }else{
      await Login({noToast: true});
      if(wx.getStorageSync('token') && wx.getStorageSync('realNameStatus') == '3'){
        _this.getDetail()
      }else if(!wx.getStorageSync('token')){
        _this.setData({
          showGetPhoneBtn: true,
        })
      }else if(wx.getStorageSync('realNameStatus') != '3'){
        _this.setData({
          showRealNameBtn: true,
        })
      }
    }
  },
})